﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ExcelFileQueryTool
{
    public sealed class ReadableExcelWorkLogGrid : ListBox
    {
        public ReadableExcelWorkLogGrid()
        {
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.Bounds);

            base.OnDrawItem(e);

            var itemValue = (ExcelWorkLogItem)this.Items[e.Index];
            string text;

            switch (itemValue.类型)
            {
                case "收入":
                case "支出":
                case "其它":
                    if (itemValue.已收付货款 == 0)
                    {
                        text = string.Format("[{0}]  {1}  {2}  {3} ({4})",
                                       itemValue.类型,
                                       itemValue.日期.ToString("yyyy/MM/dd"),
                                       itemValue.客户供应商,
                                       itemValue.货物,
                                       itemValue.备注);
                    }
                    else
                    {
                        text = string.Format("[{0}]  {1}  {2}  {3} [{4}]收/付 {5}元 ({6})",
                                       itemValue.类型,
                                       itemValue.日期.ToString("yyyy/MM/dd"),
                                       itemValue.客户供应商,
                                       itemValue.货物,
                                       itemValue.收付款人,
                                       itemValue.已收付货款,
                                       itemValue.备注);
                    }
                    break;

                default:
                    if (itemValue.已收付货款 == 0)
                    {
                        text = string.Format("[{0}]  {1}  {2}  {3} {4}{5}*{6}，{7}运 {8}元 ({9})",
                                       itemValue.类型,
                                       itemValue.日期.ToString("yyyy/MM/dd"),
                                       itemValue.客户供应商,
                                       itemValue.货物,
                                       itemValue.数量,
                                       itemValue.单位,
                                       itemValue.单价,
                                       itemValue.运输方式,
                                       itemValue.运装卸费,
                                       itemValue.备注);
                    }
                    else
                    {
                        text = string.Format("[{0}]  {1}  {2}  {3} {4}{5}*{6}，{7}运 {8}元，[{9}]收/付 {10}元 ({11})",
                                       itemValue.类型,
                                       itemValue.日期.ToString("yyyy/MM/dd"),
                                       itemValue.客户供应商,
                                       itemValue.货物,
                                       itemValue.数量,
                                       itemValue.单位,
                                       itemValue.单价,
                                       itemValue.运输方式,
                                       itemValue.运装卸费,
                                       itemValue.收付款人,
                                       itemValue.已收付货款,
                                       itemValue.备注);
                    }
                    break;
            }

            TextRenderer.DrawText(
                e.Graphics,
                text,
                new Font(SystemFonts.MessageBoxFont.FontFamily, 12, FontStyle.Regular),
                e.Bounds.Location,
                Color.Green);
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem(e);
            e.ItemHeight = 30;
        }
    }
}
